//
//  ViewController.m
//  Homework_15
//
//  Created by 徐昊 on 16/8/3.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "ViewController.h"
#import "HXTitleCollectionViewLayout.h"
#import "TitleCollectionViewCell.h"
#import "SelfSizeTool.h"
#define kCollectionViewCellName @"reuse"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, HXTileCollectionViewLayoutDelegate>
@property (nonatomic, strong) NSMutableArray *titleArray;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"网易" style:UIBarButtonItemStylePlain target:self action:nil];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    // scrollView自动边距
    // 遇到状态栏, 导航栏, 自动为页面上的某一个scrollView添加边距
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self createCollectionView];
    _titleArray = [NSMutableArray arrayWithObjects:@"头条", @"娱乐", @"热点", @"体育", @"大连", @"网易号", @"财经", @"科技", @"汽车", @"时尚", @"图片", @"跟帖", @"房产", @"直播", @"轻松一刻", @"段子", @"军事", @"历史", @"家居", @"独家", @"游戏", @"健康", @"政务", @"哒哒趣闻", @"漫画", @"彩票", @"美女", nil];
}
- (void)createCollectionView {
    HXTitleCollectionViewLayout *layout = [[HXTitleCollectionViewLayout alloc] init];
    layout.delegate = self;
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.bounds), 45) collectionViewLayout:layout];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    collectionView.bounces = NO;
    [collectionView registerNib:[UINib nibWithNibName:@"TitleCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionViewCellName];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _titleArray.count;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView
                   layout:(UICollectionViewLayout *)collectionViewLayout
                   height:(CGFloat)height
 heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_titleArray objectAtIndex:indexPath.row];
    return [SelfSizeTool selfSizeHeightWith:str height:45 font:[UIFont systemFontOfSize:17]];
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TitleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionViewCellName forIndexPath:indexPath];
    cell.title.text = _titleArray[indexPath.row];
    cell.title.textAlignment = NSTextAlignmentCenter;
    cell.backgroundColor = [UIColor cyanColor];
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
